<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

	<link rel="stylesheet" href="../../../static/font-awesome-4.7.0/css/font-awesome.min.css" >
    <link rel="stylesheet" href="../../../static/vui/css/vui.css">

    <script type="text/javascript" src="../../../static/js/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="../../../static/js/jquery.cookie.min.js"></script>
    <script type="text/javascript" src="../../../static/layer/layer.js"></script>
	<script type="text/javascript" src="../../../static/laydate/laydate.js"></script>
    <script type="text/javascript" src="../../../static/vui/js/vui.js"></script>
    <script type="text/javascript" src="../../../static/js/common.js"></script>
</head>
<body>

<div id="formArea">

	<table class="table">
		<tr>
			<td><label class="required">客户名称</label></td>
			<td><input type="text" class="input" autocomplete="off" name="name"/></td>
			<td>期初日期</td>
			<td><input type="text" class="input" autocomplete="off" n="date" name="date"/></td>
		</tr>
		<tr>
			<td><label class="required">所属机构</label></td>
			<td><div id="companyId"></div></td>
			<td>期初余额</td>
			<td><input type="text" class="input" n="money" autocomplete="off" name="balance"/></td>
		</tr>
		<tr>
			<td><label class="required">客户编号</label></td>
			<td><input type="text" class="input" autocomplete="off" name="code"/></td>
			<td>联系人</td>
			<td><input type="text" class="input" autocomplete="off" name="contact"/></td>
		</tr>
		<tr>
			<td><label class="required">客户类别</label></td>
			<td><div id="customerTypeId"></div></td>
			<td>联系电话</td>
			<td><input type="text" class="input" autocomplete="off" name="contactPhone"/></td>
		</tr>
		<tr>
			<td>联系地址</td>
			<td colspan="3">
				<textarea class="input" style="width: 578px;height: 80px;" autocomplete="off" name="address"></textarea>
			</td>
		</tr>
	</table>
</div>

<script>

var requestParams = app.getRequestParams();
$(function () {

    $('#companyId').search({
        name:'companyId',
        defaultValue:app.getLogin().companyParentId,
        click:function(){
            var index = parent.layer.getFrameIndex(window.name);
            app.window(parent.layer,{
                title:'机构选择',
                btn:[],
                area: ['800px', '600px'],
                content: '../../common/company_tree.html?index='+index+'&searchId=companyId'
            });
        }
    });

    var company = {companyId:app.getCompanyId(),companyId_text:app.getCompanyName()};
    app.resetForm(company);

    $('#customerTypeId').search({
        name:'customerTypeId',
        click:function(){
            var index = parent.layer.getFrameIndex(window.name);
            app.window(parent.layer,{
                title:'客户类别选择',
                btn:[],
                area: ['800px', '600px'],
                content: '../../common/customertype_tree.html?index='+index+'&searchId=customerTypeId'
            });
        }
    });

	if(requestParams.id){
		app.request({
			url:'customer/get',
			data:{id:requestParams.id},
			success:function(resp){
			if( resp.result==0 ){
				app.resetForm(resp.data);
				$('#formArea').append('<input type="hidden" value="'+requestParams.id+'" name="id"/>');
                $('#formArea input[name="customerTypeId_text"]').val(resp.data.customerTypeName);
                $('#formArea input[name="companyId_text"]').val(resp.data.companyName);
			}else{
				app.message(parent.layer,'获取选中信息失败【'+resp.data+'】',2);
			}
		}
		});
        laydate.render({
            elem: 'input[name="date"]',value:null
        });
	}else{
        laydate.render({
            elem: 'input[name="date"]',value:new Date()
        });
	}
});

app.getOkBtn().click(function(){
	if( !checkForm() ){
		return;
	}

	var url = 'customer/insert';

	if(requestParams.id){
		url = 'customer/update'
	}
	app.request({
		url:url,
		data:app.serializeForm(),
		success:function (resp) {
            if( resp.result==0 ){
                app.closeLastWindow();
                app.message(parent.layer,'操作成功');
                parent.$("#table").datagrid("reload");
            }else{
                app.message(layer,'操作失败【'+app.getErrorMsg(resp)+'】',2);
            }
        }
	});
});

function checkForm(){

	var checkOptions = [
		{type:'input',name:'name',verify:[{type:'required'},{type:'length',value:20}]},
        {type:'input',name:'code',verify:[{type:'required'},{type:'length',value:20}]},
        {type:'input',name:'customerTypeId_text',verify:[{type:'required'}]},
		{type:'input',name:'contact',verify:[{type:'length',value:5}]
	}];

	return app.checkForm(checkOptions);
}

</script>

</body>
</html>